package com.example.email.service;

import com.example.email.dtos.EmailDtos;
import com.example.email.entity.Mail;
import com.example.email.entity.Mailexplain;
import com.example.email.mapper.MailMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class MailService {
    @Autowired
    MailMapper mailMapper;

    /**
     * 回收站未读邮件数量
     * @param
     * @return
     */
    public int shouCount(String  receivingEmail){
        return mailMapper.shouCount(receivingEmail);
    }

    /**
     * 星标邮件未读数量
     * @param
     * @return
     */
    public int xingCount(String  receivingEmail){
        return mailMapper.xingCount(receivingEmail);
    }

    /**
     * 草稿箱数量
     * @param
     * @return
     */
    public int draftCount(String  receivingEmail){
        return mailMapper.draftCount(receivingEmail);
    }

    /**
     * 垃圾箱未读数量
     * @param
     * @return
     */
    public int deleteCount(String  receivingEmail){
        return mailMapper.deleteCount(receivingEmail);
    }

    /**
     * 已发送未读数量
     * @param
     * @return
     */
    public int faCount(String  receivingEmail){
        return mailMapper.faCount(receivingEmail);
    }

    /**
     * 已删除未读数量
     * @param
     * @return
     */
    public int shanCount(String  receivingEmail){
        return mailMapper.shanCount(receivingEmail);
    }


    /**
     * 查询收件箱里所有邮件数量
     * @param receivingEmail
     * @return
     */
    public int allCount(String receivingEmail){return  mailMapper.allCount(receivingEmail);}

    /**
     * 分页查询
     * @param receivingEmail
     * @return
     */
    public PageInfo<Mail> selectMailStatus0ByPage(String receivingEmail, Integer pageIndex, Integer pageSize, EmailDtos emailDtos) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();

        int count=0;
        int count1=0;
        int count2=0;
        int count3=0;
        //设置分页页面
        PageHelper.startPage(pageIndex,pageSize);
        List<Mail> mails=mailMapper.selectMailStatus0ByPage(receivingEmail);
        for (Mail mail : mails) {
            Date d1=sdf.parse(String.valueOf(mail.getReceivingtime()));
            Date d2=sdf.parse(sdf.format(date));
            if (d1.equals(d2)) {
                count++;
                emailDtos.setTodayCount(count);
            }else if(MailService.isYeaterday(d1,d2)==0){
                count1++;
                emailDtos.setYesterdayCount(count1);
            }else if (MailService.isThisWeek(d1)==true){
                count2++;
                emailDtos.setWeekCount(count2);
            }else {
                count3++;
                emailDtos.setErilyCount(count3);
            }
        }
        //封装一个PageInfo对象,并返回
        PageInfo<Mail> pageInfo=new PageInfo<>(mails);

        return pageInfo;
    }


    /**
     * @author LuoB.
     * @param oldTime 较小的时间
     * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     * @throws ParseException 转换异常
     */
    private static int isYeaterday(Date oldTime,Date newTime) throws ParseException{
        if(newTime==null){
            newTime=new Date();
        }
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = format.parse(todayStr);
        //昨天 86400000=24*60*60*1000 一天
        if((today.getTime()-oldTime.getTime())>0 && (today.getTime()-oldTime.getTime())<=86400000) {
            return 0;
        }
        else if((today.getTime()-oldTime.getTime())<=0){ //至少是今天
            return -1;
        }
        else{ //至少是前天
            return 1;
        }

    }


    //判断选择的日期是否是本周
    public static boolean isThisWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(String.valueOf(time)));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    /**
     * 改变多封邮件状态
     * @param mailnumber
     * @param
     * @return
     */
    public int updateMailStatus(int  [] mailnumber,int mailstatus){
        return mailMapper.updateMailStatus(mailnumber,mailstatus);
    }

    /**
     * 将多封邮件改变为星标邮件
     * @param mailnumber
     * @return
     */
    public int updateMailStarMarks(int  [] mailnumber){
        return mailMapper.updateMailStarMarks(mailnumber);
    }


    /**
     * 将多封邮件取消星标邮件
     * @param mailnumber
     * @return
     */
    public int updateMailStarMarks0(int  [] mailnumber){
        return mailMapper.updateMailStarMarks0(mailnumber);
    }

    /**
     * 单个邮箱取消标记星标邮件
     * @param ifStarmarker
     * @param mailnumber
     * @return
     */
    public int updateMailStarMark(int ifStarmarker,int mailnumber){
        return mailMapper.updateMailStarMark(ifStarmarker,mailnumber);
    }

    /**
     * 举报邮箱
     * @param mailnumber
     * @param mailstatus
     * @return
     */
    public int updateMailStatus5(int  [] mailnumber,int mailstatus){
        return mailMapper.updateMailStatus5(mailnumber,mailstatus);
    }

    /**
     * 将本页所有邮箱变为全部已读
     * @param mailnumber
     * @return
     */
    public int updateIfReadMail(int  [] mailnumber){
        return mailMapper.updateIfReadMail(mailnumber);
    }


    public int updateIfReadMails(int  [] mailnumber,int ifRead){
        return mailMapper.updateIfReadMails(mailnumber,ifRead);
    }
}
